#include "virtuallistmanager.h"
#include "customfileitem.h"
#include <QDebug>
#include <QtMath>

VirtualListManager::VirtualListManager(QObject* parent)
    : QObject(parent)
    , m_poolSize(MIN_POOL_SIZE)
{
}

VirtualListManager::~VirtualListManager()
{
    clearAllBindings();
}

int VirtualListManager::getOptimalPoolSize(int windowHeight, int itemHeight) const
{
    if (windowHeight <= 0 || itemHeight <= 0) {
        return MIN_POOL_SIZE;
    }
    
    // 计算可见item数量
    int visibleCount = qCeil(static_cast<double>(windowHeight) / itemHeight) + 1;
    
    // 加上缓冲区（上下各BUFFER_ITEMS个）
    int neededSize = visibleCount + (BUFFER_ITEMS * 2);
    
    // 限制在合理范围内
    return qBound(MIN_POOL_SIZE, neededSize, MAX_POOL_SIZE);
}

void VirtualListManager::adjustPoolSize(int newSize)
{
    // 限制范围
    newSize = qBound(MIN_POOL_SIZE, newSize, MAX_POOL_SIZE);
    
    if (newSize != m_poolSize) {
        m_poolSize = newSize;
        emit poolSizeChanged(m_poolSize);
    }
}

VirtualListManager::VisibleRange VirtualListManager::calculateVisibleRange(
    int scrollOffset, int windowHeight, int itemHeight, int totalFiles) const
{
    VisibleRange range;
    
    if (totalFiles == 0 || itemHeight <= 0) {
        range.startIndex = 0;
        range.endIndex = 0;
        range.bufferStart = 0;
        range.bufferEnd = 0;
        return range;
    }
    
    // 计算可见范围（基于滚动位置）
    range.startIndex = scrollOffset / itemHeight;
    range.endIndex = qMin((scrollOffset + windowHeight) / itemHeight + 1, totalFiles);
    
    // 添加缓冲区
    range.bufferStart = qMax(0, range.startIndex - BUFFER_ITEMS);
    range.bufferEnd = qMin(range.endIndex + BUFFER_ITEMS, totalFiles);
    
    return range;
}

void VirtualListManager::bindItemToFile(CustomFileItem* item, int fileIndex)
{
    if (!item) {
        return;
    }
    
    // 如果item已绑定到其他文件，先解除
    if (m_itemToFileIndex.contains(item)) {
        int oldIndex = m_itemToFileIndex[item];
        m_fileIndexToItem.remove(oldIndex);
    }
    
    // 如果文件已绑定到其他item，先解除
    if (m_fileIndexToItem.contains(fileIndex)) {
        CustomFileItem* oldItem = m_fileIndexToItem[fileIndex];
        m_itemToFileIndex.remove(oldItem);
    }
    
    // 建立新绑定
    m_itemToFileIndex[item] = fileIndex;
    m_fileIndexToItem[fileIndex] = item;
    
    emit bindingChanged();
}

void VirtualListManager::unbindItem(CustomFileItem* item)
{
    if (!item) {
        return;
    }
    
    if (m_itemToFileIndex.contains(item)) {
        int fileIndex = m_itemToFileIndex[item];
        m_itemToFileIndex.remove(item);
        m_fileIndexToItem.remove(fileIndex);
        emit bindingChanged();
    }
}

int VirtualListManager::getFileIndexForItem(CustomFileItem* item) const
{
    return m_itemToFileIndex.value(item, -1);
}

CustomFileItem* VirtualListManager::getItemForFileIndex(int fileIndex) const
{
    return m_fileIndexToItem.value(fileIndex, nullptr);
}

bool VirtualListManager::isItemBound(CustomFileItem* item) const
{
    return m_itemToFileIndex.contains(item);
}

void VirtualListManager::clearAllBindings()
{
    m_itemToFileIndex.clear();
    m_fileIndexToItem.clear();
    emit bindingChanged();
}

int VirtualListManager::getBoundItemCount() const
{
    return m_itemToFileIndex.size();
}

QList<int> VirtualListManager::getBoundFileIndices() const
{
    return m_fileIndexToItem.keys();
}

